package strategy;

import java.util.ArrayList;
import java.util.HashMap;

import mt.BaiduMT;
import mt.GoogleMT;
import mt.YoudaoMT;

// used to store the translation and relative featuers from a mt
public class TranslatinData {

	public String translation = null;
	
	//features begin
	public String label = null;	//google, baidu, youdao 
	public double editDistance = -1;
	public double lmLogP = -1;
	public double BLEU = -1;
	public double METEOR = -1;
	//features end

	/**
	 * 获得结果属性，对于一组三家结果。
	 * 
	 * 它主要是去调用FeatureCalc里的方法。
	 * @param translations, 对于一句话的翻译结果，{google=xxx, baidu=yyy, youdao=zzz}
	 * @return 对于一句话对应三家的句子的属性，
	 *         {google=TranslationData_g, baidu= TranslationData_b, youdao= TranslationData_y}
	 * @throws Exception
	 */
	// note that the translations may be from local records
	public static HashMap<String, TranslatinData> getTranslationDatas(
			HashMap<String, String> translations) throws Exception {
		
//		HashMap<String, Double> editDistMap = FeatureCalc.getEditDist(translations);
//		HashMap<String, Double> BLEUMap = FeatureCalc.getBLEU(translations);
		HashMap<String, Double> lmLogPMap = FeatureCalc.getLmLogP(translations);
//		HashMap<String, Double> METEORMap = FeatureCalc.getMETEOR(translations);
		
		HashMap<String, TranslatinData> results = new HashMap<String, TranslatinData>();
		for (String label : translations.keySet()) {
			TranslatinData f = new TranslatinData();
			f.label = label;
			f.translation = translations.get(label);
			f.lmLogP = lmLogPMap.get(label);
//			f.editDistance = editDistMap.get(label);
//			f.BLEU = BLEUMap.get(label);
//			f.METEOR = METEORMap.get(label);
			results.put(label, f);
		}
		return results;
	}
	
	public static HashMap<String, TranslatinData> getTranslationDatasWithRef(
			HashMap<String, String> translations, ArrayList<String> refList) throws Exception {
		



		HashMap<String, Double> METEORMap = FeatureCalc.getMETEORWithRef(translations, refList);
		
		HashMap<String, TranslatinData> results = new HashMap<String, TranslatinData>();
		
		for (String label : translations.keySet()) {
			TranslatinData f = new TranslatinData();
			f.label = label;
			f.translation = translations.get(label);
///////////////////////////////////////////			f.lmLogP = lmLogPMap.get(label);
			f.editDistance = 0;
			f.BLEU = 0;
			f.METEOR = METEORMap.get(label);
			results.put(label, f);
		}
		return results;
	}
	
	/**
	 * 获得结果属性，对于一句中文
	 * @param chinese   Chinese to be translated;
	 * @return   three translationDatas
	 * @throws Exception
	 */
	// note that the translations are from the web
	public static HashMap<String, TranslatinData> getTranslationDatas(
			String chinese) throws Exception {
		HashMap<String, String> translations = getAnswers(chinese);
		return getTranslationDatas(translations);
	}
	
	//helper
	// ON-LINE FETCH the results of Google, Baidu and Youdao
	private static HashMap<String, String> getAnswers(String chinese)
 {
		String google=null, baidu=null, youdao=null;
		HashMap<String, String> res = new HashMap<String, String>();
		try {
			google =new mt.GoogleMT().translate(chinese);
		} catch (Exception e) {
			System.err.println("Google translation failed!");
			google="";
		}
		try {
			baidu =new mt.BaiduMT().translate(chinese);
		} catch (Exception e) {
			System.err.println("Baidu translation failed!");
			baidu ="";
		}
		try {
			youdao =new mt.YoudaoMT().translate(chinese);
		} catch (Exception e) {
			System.err.println("Youdao translation failed!");
			youdao ="";
		}
		
		res.put(GoogleMT.label, google);
		res.put(BaiduMT.label, baidu);
		res.put(YoudaoMT.label, youdao);
		
		return res;
	}
}
